package com.lan.model;

import com.lan.model.symbol.This;
import com.lan.model.symbol.Variable;
import com.lan.script.FunctionObject;
import com.lan.script.ScriptObject;

public final class V implements Value {

    private Variable variable;

    private ScriptObject object;

    public V(Variable variable, ScriptObject object) {
        this.variable = variable;
        this.object = object;
    }

    @Override
    public Object getValue() {
        //对于this或super关键字，直接返回这个对象，应该是ClassObject
        if (variable instanceof This) {
            return object;
        }
        return object.meta(variable);
    }

    @Override
    public void setValue(Object value) {
        object.withMeta(variable, value);
        //如果variable是函数型变量，那改变functionObject.receiver
        if (value instanceof FunctionObject) {
            ((FunctionObject) value).setVariable(variable);
        }
    }

    @Override
    public Variable getVariable() {
        return variable;
    }

    @Override
    public ScriptObject get() {
        return object;
    }

    @Override
    public String toString() {
        return "Value of " + variable.name + " : " + getValue();
    }
}
